package cn.jetpiece.cloud.joss.app.modular.basics.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.jetpiece.cloud.joss.api.basic.ContentTypeApi;
import cn.jetpiece.cloud.joss.api.basic.model.params.CreateContentTypeParam;
import cn.jetpiece.cloud.joss.api.basic.model.params.UpdateContentTypeParam;
import cn.jetpiece.cloud.joss.api.basic.model.query.ContentTypeQuery;
import cn.jetpiece.cloud.joss.api.basic.model.result.ContentTypeResult;
import cn.jetpiece.cloud.joss.app.modular.basics.entity.ContentType;
import cn.jetpiece.cloud.joss.app.modular.basics.service.ContentTypeService;
import lombok.extern.slf4j.Slf4j;
import cn.jetpiece.cloud.model.contants.ResponseMsgConstants;
import cn.jetpiece.cloud.model.page.PageResult;
import cn.jetpiece.cloud.model.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * 内容类型控制器
 *
 * @author XuJZ
 * @since 2020-09-08 16:33:04
 */
@Slf4j
@RestController
public class ContentTypeController implements ContentTypeApi {

    @Autowired
    private ContentTypeService contentTypeService;

    @Override
    public Result<String> add(CreateContentTypeParam param) {
        if (contentTypeService.add(param)) {
            return Result.OK(ResponseMsgConstants.CREATE_SUCCESS);
        } else {
            return Result.error(ResponseMsgConstants.CREATE_ERROR);
        }
    }

    @Override
    public Result<String> update(UpdateContentTypeParam param) {
        if (contentTypeService.update(param)) {
            return Result.OK(ResponseMsgConstants.UPDATE_SUCCESS);
        } else {
            return Result.error(ResponseMsgConstants.UPDATE_ERROR);
        }
    }

    @Override
    public Result<String> delete(String fileExtension) {
        if (contentTypeService.delete(fileExtension)) {
            return Result.OK(ResponseMsgConstants.DELETE_SUCCESS);
        } else {
            return Result.error(ResponseMsgConstants.DELETE_ERROR);
        }
    }

    @Override
    public Result<ContentTypeResult> get(String fileExtension) {
        ContentType bean = contentTypeService.getById(fileExtension);
        if (bean == null) {
            return Result.OK();
        }
        ContentTypeResult result = new ContentTypeResult();
        BeanUtil.copyProperties(bean, result);
        return Result.OK(result);
    }

    @Override
    public Result<PageResult<ContentTypeResult>> page(ContentTypeQuery query) {
        return Result.OK(contentTypeService.page(query));
    }

}
